package com.fingard.dsp.bank.directbank.cmb01;

import com.fingard.dsp.bank.directbank.bankInterface.IOrderMsg;
import com.fingard.dsp.bank.reqrespobj.ordermsg.ReqOrderMsgQry;
import com.fingard.dsp.bank.reqrespobj.ordermsg.RetAtsOrderMsgQry;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/21.
 */
public class CMB01OrderMsg extends CMB01Base implements IOrderMsg {
    @Override
    public RetAtsOrderMsgQry pushOrderMsg(ReqOrderMsgQry reqOrderMsgQry) throws Exception {
        RetAtsOrderMsgQry retAts = new RetAtsOrderMsgQry(reqOrderMsgQry.reqHeader);
        retAts.writeStart();
        try {
            CMB01Header header =  new CMB01Header();
            header.FUNNAM = "TDAFORRV";
            XmlTextWriter xmlWriter =  new XmlTextWriter();
            writeCommonHeader(xmlWriter,header);
//            TDAFORRVX	输入属性	单记录
            xmlWriter.writeStartElement("TDAFORRVX");
//            直连对接企业系统客户号	COREID	C(10)	否	长安主机厂对应的10位客户号
            xmlWriter.writeElementString("COREID",bankFront.org);
//            业务参考号	YURREF	C(30)	否	数据传输，防止超时等通讯异常，需保证系统唯一
            xmlWriter.writeElementString("YURREF", header.FUNNAM+getYURREF(reqHeader.transSeqID));
//            经销商代码	DLENUM	C(40)	否
            xmlWriter.writeElementString("DLENUM",reqOrderMsgQry.DealerCode );
//            ERP订单号	ORDEID	Z(40)	否	不存在重复订单号
            xmlWriter.writeElementString("ORDEID",reqOrderMsgQry.OrderCode );
//            订单数量	ORDNUM	N	否
            xmlWriter.writeElementString("ORDNUM", reqOrderMsgQry.OrderNum);
//            合同单价	CRRAMT	M	否
            xmlWriter.writeElementString("CRRAMT",reqOrderMsgQry.OrderTotallAmount);
//            车型	CARTYP	Z(40)	可
            xmlWriter.writeElementString("CARTYP","" );
//            订单明细备注	RNOTES	Z(256)	可	请填写订单确认日期
            xmlWriter.writeElementString("RNOTES",reqOrderMsgQry.OrderConfirmDate );
            xmlWriter.writeEndElement();
//            输入	TDAFORRVY	输入属性
            while (reqOrderMsgQry.readRecord()) {
                xmlWriter.writeStartElement("TDAFORRVY");
//            汇票号	BILLNO	C(30)	否	完整票号
                xmlWriter.writeElementString("BILLNO", reqOrderMsgQry.ReqDetail.BillNo);
//            票面金额	DAFAMT	M	否
                xmlWriter.writeElementString("DAFAMT", reqOrderMsgQry.ReqDetail.BillMoney);
//            本次使用金额	USEAMT	M	否
                xmlWriter.writeElementString("USEAMT", reqOrderMsgQry.ReqDetail.UseMoney);
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
                    String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                    if("0".equals(bankRetCode)) {
//                        TDAFORRVZ
//                        返回代码	RTNCOD	C(7)	否	返回值：SCDS000则代表成功 其他值则代表失败
//                        返回信息	RTNMSG	Z(256)	可
                        String rtnCod = tmpBkReader.getFirstTextNotNull("/CMBSDKPGK/TDAFORRVZ/RTNCOD");
                        String rtnMsg = tmpBkReader.getFirstTextNotNull("/CMBSDKPGK/TDAFORRVZ/RTNMSG");
                        if("SCDS000".equals(rtnCod)) {
                            retAts.respCode ="0";
                            retAts.respInfo = rtnMsg;
                        }else{
                            while (tmpBkReader.read("/CMBSDKPGK/TDAFORRVW")){
                                rtnMsg+=tmpBkReader.readedXElement.getSingleText("ERRMSG")+";\r\n";
                            }
                            throw new Exception(rtnMsg);
                        }

                    }else{
                        String bankRetMsg =tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        throw new Exception(bankRetCode+":"+bankRetMsg);
                    }
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            retAts.respCode ="-1";
            retAts.respInfo = ex.getMessage();
        }
        retAts.writeRespCode(retAts.respCode);
        retAts.writeRespInfo(retAts.respInfo);
        retAts.writeEnd();
        return retAts;
    }
}
